//
//  ScrollViewController.m
//  PeiJue
//
//  Created by lxy on 2017/11/1.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "ScrollViewController.h"
#import <MJRefresh/MJRefresh.h>

@interface ScrollViewController ()

@end

@implementation ScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configMJRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Privates
- (void)configMJRefresh {
    __weak typeof(self) weakSelf = self;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadNewData];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    self.scrollView.mj_header = header;
    
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    // 当上拉刷新控件出现50%时（出现一半），就会自动刷新。这个值默认是1.0（也就是上拉刷新100%出现时，才会自动刷新）
    // footer.triggerAutomaticallyRefreshPercent = 0.5;
    [footer setTitle:@"--没有更多数据了--" forState:MJRefreshStateNoMoreData];
    footer.hidden = YES;
    self.scrollView.mj_footer = footer;
    
    self.hasLoadMore = NO;
    self.hasRefresh = YES;
}


#pragma mark - Publics
- (void)beginRefreshing {
    [self.scrollView.mj_header beginRefreshing];
}

- (void)endRefreshing {
    if (self.scrollView.mj_header.state == MJRefreshStateRefreshing) {
        [self.scrollView.mj_header endRefreshing];
    }
    if (self.scrollView.mj_footer.state == MJRefreshStateRefreshing) {
        [self.scrollView.mj_footer endRefreshing];
    }
}

- (void)noticeNoMoreData {
    [self.scrollView.mj_footer endRefreshingWithNoMoreData];
}

- (void)resetNoMoreData {
    [self.scrollView.mj_footer resetNoMoreData];
}

- (void)updateUI {
    if ([self.scrollView isKindOfClass:[UITableView class]]) {
        [(UITableView *)self.scrollView reloadData];
    }else if ([self.scrollView isKindOfClass:[UICollectionView class]]){
        [(UICollectionView *)self.scrollView reloadData];
    }
}

#pragma mark - Overrides
- (void)loadNewData{
    
}
- (void)loadMoreData{
    
}

#pragma mark - Getters && Setters
- (void)setHasRefresh:(BOOL)hasRefresh {
    _hasRefresh = hasRefresh;
    self.scrollView.mj_header.hidden = !hasRefresh;
}

- (void)setHasLoadMore:(BOOL)hasLoadMore {
    _hasLoadMore = hasLoadMore;
    self.scrollView.mj_footer.hidden = !hasLoadMore;
    
    // 设置上拉刷新时，先重置没有更多的数据
    if (hasLoadMore) {
        [self resetNoMoreData];
    }
}

@end
